package com.resataurant.Controller.LR;

import com.resataurant.Dto.AdminDto;
import com.resataurant.Service.AdminService;
import com.resataurant.Service.Impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");//防止中文乱码

        String user=request.getParameter("user");//获取用户账号
        String pwd=request.getParameter("pwd");//获取用户账号

        //-------------------------
        AdminService as=new AdminServiceImpl();
        AdminDto admin=as.login(user,pwd);
        int sta=admin.getSta();
        System.out.println("账号的状态码:"+sta);

        switch (sta){
            case 0:
                //失败
                request.getSession().setAttribute("sta","登录失败请输入正确的账号和密码");
                //跳转回登录界面
                response.sendRedirect("index.jsp");
                break;
            case 1:
                //管理员
                if (admin.getAdmin().getPow()==2){
                    request.getSession().setAttribute("userid", admin.getAdmin().getId());
                    response.sendRedirect("manager.jsp");
                    break;
                }
                if (admin.getAdmin().getPow()==1){
                    request.getSession().setAttribute("userid", admin.getAdmin().getId());
                    response.sendRedirect("user.jsp");
                    break;
                }
                break;
            case -1:
                //用户
                request.getSession().setAttribute("sta", "用户不存在");
                response.sendRedirect("index.jsp");
                break;
        }
    }
}
